# tools/http_tool.py
import requests

def http_get(url: str, params: dict = None) -> dict:
    response = requests.get(url, params=params)
    return response.json()

def http_post(url: str, data: dict = None) -> dict:
    response = requests.post(url, json=data)
    return response.json()

def run():
    print("\n欢迎使用 HTTP 请求工具")
    print("1. 发送 GET 请求")
    print("2. 发送 POST 请求")
    choice = input("请选择操作（1 或 2）：")
    if choice == "1":
        url = input("请输入请求的 URL：")
        params = input("请输入请求参数（字典格式，可选）：")
        try:
            params = eval(params) if params else None
            result = http_get(url, params)
            print(f"请求结果：{result}")
        except Exception as e:
            print(f"请求失败：{e}")
    elif choice == "2":
        url = input("请输入请求的 URL：")
        data = input("请输入请求数据（字典格式）：")
        try:
            data = eval(data)
            result = http_post(url, data)
            print(f"请求结果：{result}")
        except Exception as e:
            print(f"请求失败：{e}")
    else:
        print("无效选项！")